import requests
from selenium import webdriver
from bs4 import BeautifulSoup
import os
import time

class WebScraper:
    def __init__(self, url):
        self.url = url
        self.driver = webdriver.Chrome()  # 确保 ChromeDriver 已安装

    def fetch_static_content(self):
        response = requests.get(self.url)
        return response.text

    def fetch_dynamic_content(self):
        self.driver.get(self.url)
        time.sleep(5)  # 等待动态内容加载
        return self.driver.page_source

    def parse_html(self, html_content):
        soup = BeautifulSoup(html_content, 'html.parser')
        return soup

    def save_media(self, media_url, media_type):
        file_name = os.path.join("static", media_type, os.path.basename(media_url))
        with open(file_name, 'wb') as f:
            f.write(requests.get(media_url).content)

    def extract_text(self, soup):
        return soup.get_text()

    def close(self):
        self.driver.quit()
